package com.study.scala.collections.list

object TestList2 {
  def main(args: Array[String]): Unit = {
    // println(msort((x: Int, y: Int) => x > y)(List(1, 5, 8, 3, 9)))

    val list = List("zhao", "shi", "chao");
    println(list.map(c => c.toList))
    println(list.flatMap(c => c.toList))
    println(List.range(1, 5).flatMap(i => List.range(1, i).map(j => (i, j))))
  }

  def msort[T](less: (T, T) => Boolean)(xs: List[T]): List[T] = {
    def merge(xs: List[T], ys: List[T]): List[T] = (xs, ys) match {
      case (Nil, _) => ys
      case (_, Nil) => xs
      case (x :: xs1, y :: ys1) => if (less(x, y)) {
        x :: merge(xs1, ys);
      } else {
        y :: merge(xs, ys1);
      }
    }

    val n = xs.length / 2;
    if (n == 0) {
      xs
    } else {
      val (ys, zs) = xs splitAt (n)
      merge(msort(less)(ys), msort(less)(zs))
    }
  }
}